package fr.xgouchet.wedding.client.controller;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.RootPanel;

import fr.xgouchet.wedding.client.event.FeedbackMessageEvent;
import fr.xgouchet.wedding.client.i18n.Constants;
import fr.xgouchet.wedding.client.presenter.AgendaPresenter;
import fr.xgouchet.wedding.client.presenter.GuestsListPresenter;
import fr.xgouchet.wedding.client.presenter.Presenter;
import fr.xgouchet.wedding.client.presenter.SettingsPresenter;
import fr.xgouchet.wedding.client.view.TaskFormView;
import fr.xgouchet.wedding.client.view.TasksListView;
import fr.xgouchet.wedding.client.view.GuestFormView;
import fr.xgouchet.wedding.client.view.GuestsListView;
import fr.xgouchet.wedding.client.view.SettingsView;

/**
 * Controller for the main app
 * 
 * @author x.gouchet
 * 
 */
public class MainController implements Presenter, ValueChangeHandler<String> {

	/**
	 * Constructor
	 * 
	 * @param eventBus
	 *            the event bus for this App Controller
	 */
	public MainController(HandlerManager eventBus) {
		mEventBus = eventBus;
		bind();
	}

	/**
	 * @see Presenter#go(HasWidgets)
	 */
	public void go(HasWidgets container) {
		mContainer = container;

		if (History.getToken().length() == 0) {
			History.newItem("home");
			return;
		}

		History.fireCurrentHistoryState();
	}

	/**
	 * @see ValueChangeHandler#onValueChange(ValueChangeEvent)
	 */
	public void onValueChange(ValueChangeEvent<String> event) {
		// token changed, erase previous messages
		mEventBus.fireEvent(new FeedbackMessageEvent("", false));

		// get new token
		String token = event.getValue();
		String title = "Webding";

		if (token != null) {
			Presenter presenter = null;

			if (token.equals("settings")) {
				presenter = new SettingsPresenter(mEventBus, new SettingsView());
				title = Constants.I18N.linkSettings();
			}

			if (token.equals("agenda")) {
				presenter = new AgendaPresenter(mEventBus, new TasksListView(),
						new TaskFormView());
				title = Constants.I18N.linkAgenda();
			}

			if (token.equals("guests-list")) {
				presenter = new GuestsListPresenter(mEventBus,
						new GuestsListView(), new GuestFormView());
				title = Constants.I18N.linkGuests();
			}

			if (presenter != null)
				presenter.go(mContainer);
			RootPanel.get("title").clear();
			RootPanel.get("title").add(new HTML(title));
		}

	}

	/**
	 * Binds the controller to History and event bus
	 */
	private void bind() {
		History.addValueChangeHandler(this);
	}

	/**
	 * Event Bus used to handle all communication between different parts of the
	 * module
	 */
	private final HandlerManager mEventBus;

	/**
	 * The container of this app controller
	 */
	private HasWidgets mContainer;
}
